module(..., package.seeall)

require "nvm"
require "mqttOutMsgKeson"
require "mqttOutMsg"
require "getLbsLoc"
require "modbusrtu"
require "terminalSendTheme"
-- 初始化nvm获取配置参数
nvm.init("config.lua", true)

-- 系统参数初始化
local clientNo, pKey, sysId, sn, devtype, dc, pressureUnit, decimalPoint,
      pressValue, mode, lAlarmOrNot = nvm.get("ClientNo"), nvm.get("PKey"),
                                      nvm.get("SysId"), nvm.get("Sn"),
                                      nvm.get("Devtype"), nvm.get("Dc"),
                                      nvm.get("PressureUnit"),
                                      nvm.get("DecimalPoint"),
                                      nvm.get("PressValue"), nvm.get("Mode"),
                                      nvm.get("LAlarmOrNot")
DataUploadTaskStat = nil
-- 其他参数初始化
local errInfo, sysTime, seq, slaveaddr, instructions, reg, sendTopic,
      sendMqttData, mqttEnable = "", 0, "", "0x01", "0x03", "", "", "", true

-- 设备端参数配置修改
local alarmNew = nil
local function timedAcquisitionOfPressure()
    log.info("timedAcquisitionOfPressure定时任务！！", "lAlarmOrNot:",
             lAlarmOrNot)
    sysTime = os.time() .. "000"
    sendTopic, sendMqttData =
        terminalSendTheme.quipmentReal_timeDataAssembly_keson(sysTime, alarmNew)
    mqttOutMsgKeson.insertMsgKeson(sendTopic, sendMqttData, 0)
    sendTopic, sendMqttData = terminalSendTheme.quipmentReal_timeDataAssembly(
                                  sysTime, alarmNew)
    mqttOutMsg.insertMsgUser(sendTopic, sendMqttData, 0)
end
-- 实时上传数据任务停止
function stopTimeDataUploadTask()
    if DataUploadTaskStat then sys.timerStop(DataUploadTaskStat) end
end
-- 实时上传数据任务
function realTimeDataUpload(dcSet)
    log.info("dcSet", dcSet)
    if dcSet > 0 then
        log.info("实时数据上传")
        if modbusrtu.Alarm == "0" then
            log.info("正常数据上传！！")
            if DataUploadTaskStat then
                sys.timerStop(DataUploadTaskStat)
            end
            DataUploadTaskStat = sys.timerLoopStart(timedAcquisitionOfPressure,
                                                    dcSet * 1000)
        elseif modbusrtu.Alarm == "1" or modbusrtu.Alarm == "2" then
            if lAlarmOrNot == false  then
                log.info("正常数据上传2！！")
                if DataUploadTaskStat then
                    sys.timerStop(DataUploadTaskStat)
                end
                DataUploadTaskStat = nil
            else
                if DataUploadTaskStat then
                    sys.timerStop(DataUploadTaskStat)
                end
                DataUploadTaskStat = sys.timerLoopStart(
                                         timedAcquisitionOfPressure,
                                         dcSet * 1000)
            end
        else
            if DataUploadTaskStat then
                sys.timerStop(DataUploadTaskStat)
            end
            DataUploadTaskStat = sys.timerLoopStart(timedAcquisitionOfPressure,
                                                    dcSet * 1000)
        end
    end
end

realTimeDataUpload(dc)
